package com.wy.jarscan;

import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author wangyong
 * @version 1.0
 * @description
 * @date 2023-10-31 15:41
 */
public class JarCodeChecker {

    private static final String SET_AUTO_COMMIT_METHOD = "makeGetRequest";

    public static void main(String[] args) throws IOException {
        String classpath = System.getProperty("java.class.path");
        String[] classpathEntries = classpath.split(File.pathSeparator);

        for (String classpathEntry : classpathEntries) {
            File file = new File(classpathEntry);
            if (needSearch(file)) {
                scanJarFile(file.getPath());
            }
        }
    }

    /**
     * 是否需要查找
     *
     * @param file 文件
     * @return 是否
     */
    public static boolean needSearch(File file) {
        if (!file.isFile()) {
            return false;
        }

        String name = file.getName();
        if (!name.endsWith(".jar")) {
            return false;
        }

        return name.contains("qm-erp");
    }


    private static void scanJarFile(String jarFilePath) throws IOException {
        System.out.println("开始扫描：" + jarFilePath);
        try (JarFile jarFile = new JarFile(jarFilePath)) {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
                    try (InputStream inputStream = jarFile.getInputStream(entry)) {
                        scanClassFile(inputStream);
                    }
                }
            }
        }
    }

    private static void scanClassFile(InputStream inputStream) throws IOException {
        ClassReader classReader = new ClassReader(inputStream);
        ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
                MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
                return new MethodVisitor(Opcodes.ASM5, methodVisitor) {
                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
                        if (name.equals(SET_AUTO_COMMIT_METHOD)) {
                            System.out.println("Found setAutoCommit(false) method call in class: " + classReader.getClassName());
                        }
                        super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
                    }
                };
            }
        };
        classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
    }
}
